using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using FiltronASP.Modules.Abstracts;
using FiltronASP.Models;
using FiltronASP.Models.DAO;
using FiltronASP.Helpers.Translations;
using FiltronASP.Models.DAO.Interfaces;
using FiltronASP.Models.DAO.Extensions;
using System.Text;

namespace FiltronASP.Modules
{

    public class normalNewsMainModule : AbstractModule
    {

        public override string GenerateContent(FiltronASP.Models.Page page)
        {
            int languageId = Convert.ToInt32(page.language_id);
            int newsCounter = 0;
            bool ThereAreAnyNewses = false;
            StringBuilder sb = new StringBuilder();
            sb.Append(String.Format("<h3>{0}</h3>", translationHelper.getTranslation("_normalNewsTitle", languageId)));
            sb.Append(String.Format("<img src='{0}' alt='divider'>", VirtualPathUtility.ToAbsolute("~/Content/img/line.png")));
            sb.Append("<div class='carousel-inner' role='listbox'>");
            IPageDAO PageDAO = new PageDAO();
            IQueryable<Page> pagesCollection = PageDAO.FindByLanguageAndTypeNameAndPublished(languageId, "textNews", true);
            if (pagesCollection != null) { 
                int allNewsCounter = (pagesCollection.Count() - 1);
                foreach (Page singleNews in pagesCollection) {
                    ThereAreAnyNewses = true;
                    if (newsCounter == 0) {
                        sb.Append("<div class='item active'>");
                        sb.Append("<div class='col-md-12 col-sm-12 col-xs-12 no-padding'>");
                    } else if ((newsCounter % 2) == 0) {    //if
                        sb.Append("<div class='item'>");
                        sb.Append("<div class='col-md-12 col-sm-12 col-xs-12 no-padding'>");
                    }   //else
                    sb.Append(String.Format("<a href='{0}'>", singleNews.page_link));
                    sb.Append("<div class='col-lg-6 col-md-12 col-sm-12 col-xs-12 each-note-carousel no-padding'>");
                    sb.Append("<div class='col-xs-2 no-padding date'>");
                    IVariableValueDAO varValDAO = new VariableValueDAO();
                    //zawartosc
                    VariableValue shortNews = varValDAO.FindByVariableAndPage("newsContentShort", Convert.ToInt32(singleNews.page_id), Convert.ToInt32(singleNews.language_id));
                    String contentString = String.Empty;
                    if (shortNews != null) {
                        contentString = shortNews.variable_value;
                    }   //if
                    //miesiac
                    VariableValue shortNewsMonth = varValDAO.FindByVariableAndPage("newsMonth", Convert.ToInt32(singleNews.page_id), Convert.ToInt32(singleNews.language_id));
                    String month = String.Empty;
                    if (shortNewsMonth != null)
                    {
                        month = shortNewsMonth.variable_value;
                    }   //if
                    //dzien
                    VariableValue shortNewsDay = varValDAO.FindByVariableAndPage("newsDay", Convert.ToInt32(singleNews.page_id), Convert.ToInt32(singleNews.language_id));
                    String day = String.Empty;
                    if (shortNewsDay != null)
                    {
                        day = shortNewsDay.variable_value;
                    }   //if
                    sb.Append(String.Format("<p class='big-date'>{0}</p>", day));
                    sb.Append(String.Format("<p class='small-date'>{0}</p>", month));
                    sb.Append("</div>");
                    sb.Append("<div class=' col-xs-10 news-home-content'>");
                    sb.Append(String.Format("<h4>{0}</h4>", singleNews.page_name));
                    sb.Append(String.Format("<p>{0}</p>", contentString));
                    sb.Append("</div>");
                    sb.Append("</div>");
                    sb.Append("</a>");
                    if ((((newsCounter % 2) == 1) && (newsCounter > 0)) || (allNewsCounter == newsCounter)) {    //if
                        sb.Append("</div>");
                        sb.Append("</div>");
                    }   //else
                    newsCounter++;
                    if (newsCounter >= 4)
                    {
                        break;
                    }   //if
                }
            }           
            sb.Append("</div>");
            return (ThereAreAnyNewses == true) ? sb.ToString() : String.Empty;
        }
    }
}
